/*
 * @Author: yaoling 1871883713@qq.com
 * @Date: 2022-10-14 16:41:23
 * @LastEditors: yaoling 1871883713@qq.com
 * @LastEditTime: 2022-12-23 15:58:52
 * @FilePath: \company-world\src\router\index.ts
 * @Description: 筑云内网
 */
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Home from '@/views/home/index.vue'
import Login from '@/views/login/newlogin/login.vue'
import { getToken } from '@/utils/token-cache'

let baseRoute: Array<RouteRecordRaw> = [
  {
    path: '/',
    meta: {
      title: '企业文档管理'
    },
    redirect: '/home'
  },
  {
    path: '/login',
    meta: {
      title: '登录'
    },
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    meta: {
      title: '企业文档管理',
      icon: 'iconqiye'
    },
    component: Home,
    redirect: '/login',
    children: [
      {
        path: '/home/home-page',
        meta: {
          title: '工作台',
          icon: 'icona-gongzuotaidiannaofill'
        },
        name: 'HomePage',
        component: () => import('@/views/homepage/index.vue')
      }
    ]
  }
]
/**
 * @desc:获取文件夹路由
 */
const files = require.context('@/router/modules', true, /\.ts$/)
files.keys().forEach((key) => {
  files(key).default.forEach((item: RouteRecordRaw, index: number) => {
    const name = Object.keys(files(key).default)[index]
    const routeItem = files(key).default[name]
    baseRoute = baseRoute.concat(routeItem)
  })
})

export const routes = baseRoute
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

/**
 * @description: 路由拦截
 * @param {*} to
 * @return {*}
 */
// const token = getToken()
// router.beforeEach((to) => {
//   if (to.path !== '/login') {
//     if (!token) {
//       return '/login'
//     }
//   }
// })
export default router
